var GateWayModule = angular.module("GateWayModule", []);

/**
 * 主控制器
 */
GateWayModule.controller('mainCtrl',  function($scope, $http){
    $scope.init = function() {
        $scope.unFinishWorkitem = true;
        $scope.unReadWorkItem = true;
        $scope.MyInstanceStu = false;
        $scope.GetUnfinishWorkItems();
        $scope.GetUnReadWorkItems();
        $scope.GetFinishWorkItems();
        $scope.GetReadWorkItems();
        $scope.LoadNoteData();
        $scope.LoadCrmData();
    };


    // 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
    Date.prototype.Format = function(fmt)
    { //author: meizz
        var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }


    /**
     * 获取待办
     */
    $scope.GetUnfinishWorkItems = function() {
        $http({
            method: 'GET',
            url: "/Portal/WorkItem/GetUnfinishWorkItems",
            params: {
                sEcho:1,
                iColumns:6,
                sColumns:",,,,,",
                iDisplayStart:0,
                iDisplayLength:10,
                mDataProp_0:"Priority",
                mDataProp_1:"InstanceName",
                mDataProp_2:"DisplayName",
                mDataProp_3:"ReceiveTime",
                mDataProp_4:"OriginatorName",
                mDataProp_5:"OriginatorOUName",
                keyWord:""
            }

        }).success(function(result) {
            $scope.unfinishWorkItems = result.Rows;
        });
    };

    /**
     * 获取已办
     */
    $scope.GetFinishWorkItems = function () {
        var endDate =new Date();
        var startDate =new Date();
        startDate.setMonth(endDate.getMonth()-1);
        $http({
            method: 'GET',
            url: "/Portal/WorkItem/GetFinishWorkItems",
            params: {
                sEcho: 1,
                iColumns: 6,
                sColumns: ",,,,,",
                iDisplayStart: 0,
                iDisplayLength: 10,
                mDataProp_0: "InstanceName",
                mDataProp_1: "DisplayName",
                mDataProp_2: "ReceiveTime",
                mDataProp_3: "FinishTime",
                mDataProp_4: "OriginatorName",
                mDataProp_5: "OriginatorOUName",
                startTime: startDate.Format("yyyy-MM-dd"),
                endTime: endDate.Format("yyyy-MM-dd"),
                workflowCodes:"",
                instanceName:""
    }

        }).success(function (result) {
            $scope.finishedWorkItems = result.Rows;
        });
    };


    /**
     * 获取待阅
     */
    $scope.GetUnReadWorkItems = function() {
        $http({
            method: 'GET',
            url: "/Portal/CirculateItem/GetUnReadWorkItems",
            params: {
                sEcho:1,
                iColumns:6,
                sColumns:",,,,,",
                iDisplayStart:0,
                iDisplayLength:10,
                mDataProp_0:"ObjectID",
                mDataProp_1:"InstanceName",
                mDataProp_2:"DisplayName",
                mDataProp_3:"ReceiveTime",
                mDataProp_4:"OriginatorName",
                mDataProp_5:"OriginatorOUName",
                keyWord:""
            }

        }).success(function(result) {
            $scope.unReadWorkItems = result.Rows;
        });
    };

    /**
     * 获取我的流程
     */
    $scope.QueryMyInstance = function() {
        $http({
            method: 'GET',
            url: "/Portal/Instance/QueryMyInstance",
            params: {
                sEcho: 1,
                iColumns: 8,
                sColumns: ",,,,,,,",
                iDisplayStart: 0,
                iDisplayLength: 10,
                mDataProp_0: "Priority",
                mDataProp_1: "InstanceName",
                mDataProp_2: "WorkflowName",
                mDataProp_3: "CreatedTime",
                mDataProp_4: "FinishedTime",
                mDataProp_5: "FinishedTime",
                mDataProp_6: "ApproverLink",
                mDataProp_7: "Approver",
                state: 0
            }
        }).success(function(result) {
            $scope.MyInstance = result.Rows;
        });
    };

    /**
     * 获取已阅
     */
    $scope.GetReadWorkItems = function() {
        $http({
            method: 'GET',
            url: "/Portal/CirculateItem/GetReadWorkItems",
            params: {
                sEcho:1,
                iColumns:6,
                sColumns:",,,,,",
                iDisplayStart:0,
                iDisplayLength:10,
                mDataProp_0:"ObjectID",
                mDataProp_1:"InstanceName",
                mDataProp_2:"DisplayName",
                mDataProp_3:"ReceiveTime",
                mDataProp_4:"OriginatorName",
                mDataProp_5:"OriginatorOUName",
                keyWord:""
            }

        }).success(function(result) {
            $scope.readWorkedItems = result.Rows;
        });
    };


    /**
     * 获取通知公告
     */
    $scope.LoadNoteData = function() {
        $http({
            method : 'GET',
            url : "/Portal/kumhosunny/gateway/loadNoticeData",
            params : {
                "pageSize" : 15
            }
        }).success(function(result) {
            if(result.Success) {
                $scope.notices = result.Extend;
            } else {

            }
        });
    };



    $scope.LoadCrmData = function() {
        $http({
            method : 'GET',
            url : "/Portal/CrmSum/getCrmSumData"
        }).success(function(result) {
            if(result.status=="200") {
                $scope.crmData = result.data;
            } else {

            }
        });
    };




    $scope.typeChange = function(type){
        if (type == "unFinishWorkitem") {
            $scope.unFinishWorkitem = true;
            $scope.MyInstanceStu=false;
            $scope.GetUnfinishWorkItems();
        } else if (type == "finishedWorkitem") {
            $scope.unFinishWorkitem = false;
            $scope.MyInstanceStu=false;
            $scope.GetFinishWorkItems();
        } else if (type == "unReadWorkItem") {
            $scope.unReadWorkItem = true;
            $scope.GetUnReadWorkItems();
        } else if (type == "readedWorkItem") {
            $scope.unReadWorkItem = false;
            $scope.GetReadWorkItems();
        } else if (type == "QueryMyInstance") {
            $scope.MyInstanceStu=true;
            $scope.QueryMyInstance();
        }
    }




    $scope.init();







});










